package mfuny.ggongal.admin.games;

import java.io.File;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import mfuny.ggongal.admin.Server;
import mfuny.ggongal.admin.memo.MemoDao;
import mfuny.ggongal.admin.page.PageDao;
import mfuny.ggongal.admin.page.Pager;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import com.ibatis.sqlmap.client.SqlMapClient;

public class GameService {
	SqlMapClient sqlMapper;
	ModelAndView mav;
	
	GameDao resultClass;
	GameDao paramClass;
	List<MemoDao> memoList;
	List<GameDao> resultList;
	List<GameDao> allList;
	List<MultipartFile> files;
	MultipartFile file;
	
	Random ran;
	
	Pager page;
	PageDao pageClass;
	HashMap searchMap;
	
	
	int pageSize = 10;
	int pageGroupSize = 10;
	
	
	public GameService() throws Exception {
		sqlMapper = Server.getSqlMapper( );
		mav = new ModelAndView();
		resultClass = new GameDao();
		paramClass = new GameDao();
		memoList = new ArrayList<MemoDao>();
		resultList = new ArrayList<GameDao>();
		allList = new ArrayList<GameDao>();
		searchMap = new HashMap();
		page = new Pager();
		pageClass = new PageDao();
		ran = new Random();
	}
	
	public void gamesInsert( GameDao gameDao ) throws Exception {
		gameDao.setRegdate( new Timestamp( System.currentTimeMillis( ) ) );
		files = gameDao.getGamefiles( );
		int i = 0;
		MultipartFile file = files.get(i);
		gameDao.setImgmain_oriname( file.getOriginalFilename( ) );
		gameDao.setImgmain_capacity(file.getSize());
		gameDao.setImgmain_savname( String.valueOf( System.currentTimeMillis( ) ) + "_" + ran.nextInt( 100 ) + i++ + ".jpg" );
		file = files.get(i);
		gameDao.setImgdetail1_oriname( file.getOriginalFilename( ) );
		gameDao.setImgdetail1_capacity(file.getSize());
		gameDao.setImgdetail1_savname( String.valueOf( System.currentTimeMillis( ) ) + "_" + ran.nextInt( 100 ) + i++ + ".jpg" );
		file = files.get(i);
		gameDao.setImgdetail2_oriname( files.get( i ).getOriginalFilename( ) );
		gameDao.setImgdetail2_capacity(file.getSize());
		gameDao.setImgdetail2_savname( String.valueOf( System.currentTimeMillis( ) ) + "_" + ran.nextInt( 100 ) + i++ + ".jpg" );
		file = files.get(i);
		gameDao.setImgdetail3_oriname( files.get( i ).getOriginalFilename( ) );
		gameDao.setImgdetail3_capacity(file.getSize());
		gameDao.setImgdetail3_savname( String.valueOf( System.currentTimeMillis( ) ) + "_" + ran.nextInt( 100 ) + i++ + ".jpg" );
		try {
			sqlMapper.startTransaction( );
			i = sqlMapper.update( "games_insert", gameDao );
			if ( i > 0 ) {
				i = 0;
				files.get( i++ ).transferTo( new File( Server.filePath + gameDao.getImgmain_savname( ) ) );
				files.get( i++ ).transferTo( new File( Server.filePath + gameDao.getImgdetail1_savname( ) ) );
				files.get( i++ ).transferTo( new File( Server.filePath + gameDao.getImgdetail2_savname( ) ) );
				files.get( i++ ).transferTo( new File( Server.filePath + gameDao.getImgdetail3_savname( ) ) );
				sqlMapper.commitTransaction( );
			}
		} finally {
			sqlMapper.endTransaction( );
		}
	}
	
//	@SuppressWarnings( "unchecked" )
//	public ModelAndView gamesList() throws SQLException {
//		resultList = sqlMapper.queryForList( "games_selectAll", null );
//		mav.clear( );
//		mav.setViewName( "/games/gamesList.ga" );
//		mav.addObject( "resultList", resultList );
//		mav.addObject( "allList", resultList );
//		mav.addObject( "gamesCount", resultList.size() );
//		return mav;
//	}
	@SuppressWarnings( "unchecked" )
	public ModelAndView gamesList( String pageNum ) throws Exception {
		pageClass = page.getPageClass( "gamesList", pageNum, pageSize, pageGroupSize, "games_pageListAddCount", "games_countAll" );
		resultList = sqlMapper.queryForList( "games_selectAll", null );
		mav.clear( );
		mav.setViewName( "/games/gamesList.ga" );
		mav.addObject( "pageClass", pageClass );
		mav.addObject( "allList", resultList );
		return mav;
	}
	
	public View gamesUpdateStatus( GameDao gameDao ) throws SQLException {
		sqlMapper.update( "games_updateStatus", gameDao );
		return new RedirectView("./gamesList.ga");
	}
	
	@SuppressWarnings( "unchecked" )
	public ModelAndView gamesMemoPopup( MemoDao memoDao ) throws SQLException {
		memoList = sqlMapper.queryForList( "memo_selectOne", memoDao );
		mav.clear( );
		mav.setViewName( "/games/gamesMemoPopup" );
		mav.addObject( "memoList", memoList );
		mav.addObject( "memoClass", memoDao );
		mav.addObject( "listSize", memoList.size( ) );
		return mav;
	}
	
	public ModelAndView regGamesMemo( MemoDao memoDao ) throws Exception {
		memoDao.setRegdate( new Timestamp( System.currentTimeMillis( )) );
		sqlMapper.update( "memo_insert", memoDao );
		mav.setViewName( "/popupClose" );
		return mav;
	}
	
	@SuppressWarnings( "unchecked" )
	public ModelAndView searchGames( String pageNum, String datepick1, String datepick2, String status, String no, String name ) throws Exception {
		searchMap.clear( );
		searchMap.put( "datepick1", datepick1.equals( "" ) ? "1970-01-01" : datepick1 );
		searchMap.put( "datepick2", datepick2.equals( "" ) ? "2037-12-01" : datepick2 );
		searchMap.put( "status",  status );
		searchMap.put( "name", name );
		
		if ( no.equals( "" ) || no == null ) {
//			resultList = sqlMapper.queryForList( "games_searchWithoutNo", searchMap );
			pageClass = page.getPageClass( "searchGames", pageNum, pageSize, pageGroupSize, "games_searchWithoutNoAddCount", "games_searchCountWithoutNo", searchMap );
		} else {
			searchMap.put( "no", no );
//			resultList = sqlMapper.queryForList( "games_search", searchMap );
			pageClass = page.getPageClass( "searchGames", pageNum, pageSize, pageGroupSize, "games_searchAddCount", "games_searchCount", searchMap );
		}
		resultList = sqlMapper.queryForList( "games_selectAll", null );
		mav.clear( );
		mav.setViewName( "/games/gamesList.ga" );
		mav.addObject( "pageClass", pageClass );
		mav.addObject( "allList", resultList );
		mav.addObject( "datepick1", datepick1 );
		mav.addObject( "datepick2", datepick2 );
		mav.addObject( "status", status );
		mav.addObject( "no", no );
		mav.addObject( "name", name );
		return mav;
	}
	
	public ModelAndView detailGames( int no ) throws Exception {
		resultClass = (GameDao)sqlMapper.queryForObject( "games_selectNo", no );
		mav.clear( );
		mav.setViewName( "/games/detailGames" );
		mav.addObject( "resultClass", resultClass );
		return mav;
	}
	
	public ModelAndView modifyGamesForm( int no ) throws Exception {
		resultClass = (GameDao)sqlMapper.queryForObject( "games_selectNo", no );
		mav.clear( );
		mav.setViewName( "/games/modifyGamesForm" );
		mav.addObject( "resultClass", resultClass );
		return mav;
	}
	
	public ModelAndView modifyGames( GameDao gameDao ) throws Exception {
		GameDao beforeClass = (GameDao)sqlMapper.queryForObject( "games_selectNo", gameDao.getNo( ) );
		files = gameDao.getGamefiles( );
		String oriName = "";
		int i = 0;
		MultipartFile file = files.get(i);
		oriName = file.getOriginalFilename( );
		gameDao.setImgmain_capacity(file.getSize());
		gameDao.setImgmain_oriname( !oriName.equals( "" ) ? oriName : null );
		gameDao.setImgmain_savname( !oriName.equals( "" ) ? String.valueOf( System.currentTimeMillis( ) ) + "_" + ran.nextInt( 100 ) + i + ".jpg" : null );
		file = files.get( ++i );
		oriName = file.getOriginalFilename( );
		gameDao.setImgdetail1_capacity(file.getSize());
		gameDao.setImgdetail1_oriname( !oriName.equals( "" ) ? oriName : null );
		gameDao.setImgdetail1_savname( !oriName.equals( "" ) ? String.valueOf( System.currentTimeMillis( ) ) + "_" + ran.nextInt( 100 ) + i + ".jpg" : null );
		file = files.get( ++i );
		oriName = file.getOriginalFilename( );
		gameDao.setImgdetail2_capacity(file.getSize());
		gameDao.setImgdetail2_oriname( !oriName.equals( "" ) ? oriName : null );
		gameDao.setImgdetail2_savname( !oriName.equals( "" ) ? String.valueOf( System.currentTimeMillis( ) ) + "_" + ran.nextInt( 100 ) + i + ".jpg" : null );
		file = files.get( ++i );
		oriName = file.getOriginalFilename( );
		gameDao.setImgdetail3_capacity(file.getSize());
		gameDao.setImgdetail3_oriname( !oriName.equals( "" ) ? oriName : null );
		gameDao.setImgdetail3_savname( !oriName.equals( "" ) ? String.valueOf( System.currentTimeMillis( ) ) + "_" + ran.nextInt( 100 ) + i + ".jpg" : null );
		try {
			sqlMapper.startTransaction( );
			i = sqlMapper.update( "games_modify", gameDao );
			if ( i > 0 ) {
				i = 0;
				if ( gameDao.getImgmain_oriname( ) != null ) { files.get( i ).transferTo( new File( Server.filePath + gameDao.getImgmain_savname( ) ) ); }
				i++;
				if ( gameDao.getImgdetail1_oriname( ) != null ) { files.get( i ).transferTo( new File( Server.filePath + gameDao.getImgdetail1_savname( ) ) ); }
				i++;
				if ( gameDao.getImgdetail2_oriname( ) != null ) { files.get( i ).transferTo( new File( Server.filePath + gameDao.getImgdetail2_savname( ) ) ); }
				i++;
				if ( gameDao.getImgdetail3_oriname( ) != null ) { files.get( i ).transferTo( new File( Server.filePath + gameDao.getImgdetail3_savname( ) ) ); }

				if ( gameDao.getImgmain_oriname( ) != null ) { new File( Server.filePath + beforeClass.getImgmain_savname( ) ).delete( ); }
				if ( gameDao.getImgdetail1_oriname( ) != null ) { new File( Server.filePath + beforeClass.getImgdetail1_savname( ) ).delete( ); }
				if ( gameDao.getImgdetail2_oriname( ) != null ) { new File( Server.filePath + beforeClass.getImgdetail2_savname( ) ).delete( ); }
				if ( gameDao.getImgdetail3_oriname( ) != null ) { new File( Server.filePath + beforeClass.getImgdetail3_savname( ) ).delete( ); }
				
				sqlMapper.commitTransaction( );
			}
		} finally {
			sqlMapper.endTransaction( );
		}
		mav.clear( );
		mav.setViewName( "/popupClose" );
		return mav;
	}
	
	public GameDao getGameDao(int game_no) throws Exception {
		GameDao game = (GameDao) sqlMapper.queryForObject( "games_selectNo", game_no );
		return game;
	}
	//test2.
	public Object getBeanForJson(String sqlId, Object param) throws Exception {
		Object obj = sqlMapper.queryForObject( sqlId, param );
		return obj;
	}
	//test3.
	public List<Object> getBeanForList(String sqlId, Object param) throws Exception {
		@SuppressWarnings( "unchecked" )
		List<Object> list = sqlMapper.queryForList( sqlId, param );
		return list;
	}
}
